<template>
	<view class="content">
		<view class="good-search">
			<view class="search-bar">
				<text class="iconfont icon-sousuo"></text>
				<input class="input"  v-model="keyWord" @confirm="onSearch" @input="onInput" confirm-type="search" placeholder-style="color: #9C9D9E;font-size: 28rpx;" placeholder="输入关键词搜索" />
				<text class="iconfont icon-guanbi" :class="[{ show : isShowClose}]" @tap="clearKeyword"></text>		
			</view>
			<view class="search-btn" @tap="onSearch()">搜索</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'search',
		data() {
			return {
				keyWord: null,
				isShowClose: false,
			}
		},
		onLoad() {
			
		},
		methods: {
			// 触发关键词更新
			onInput(e) {
				if (e.detail.value) {
					this.isShowClose = true;
					this.keyWord = e.detail.value;
				} else {
					this.isShowClose = false;
					this.keyWord = null;
				}
			},
			// 清除关键词
			clearKeyword() {
				this.keyWord = null;
				this.isShowClose = false;
			},
			// 触发商品搜索事件
			onSearch() {
				if (!this.keyWord) {
					uni.showToast({
						title: '请输入搜索关键词',
						icon: 'none'
					})
					return false;
				}
				uni.navigateTo({
					url: '/pages/goods/result?keyWord=' + this.keyWord
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		.good-search {
			background: #fff;
			padding: 20rpx 16rpx;
			@include flexBox()
			.search-bar {
				width: 708rpx;
				height: 60rpx;
				border-radius: 50rpx;
				background: #fff;
				padding: 0 26rpx;
				border: 2rpx solid #FF4088;
				box-sizing: border-box;
				color: #9C9D9E;
				margin: 0 auto;
				flex: 1;
				@include flexBox(space-between, center);
				.icon-sousou {
					font-size: 40rpx;
				}
				.text {
					font-size: 28rpx;
					color: #9C9D9E;
				}
				.icon-guanbi {
					font-size: 30rpx;
					margin-left: 10rpx;
					transition: 0.5s;
					opacity: 0;
					&.show {
						opacity: 1;
					}
				}
				.input {
					font-size: 28rpx;
					color: #9C9D9E;
					flex: 1;
					text-align: center;
					background: none;
				}
			}
			.search-btn {
				background: #008000;
				border-radius: 50rpx;
				color: #fff;
				font-size: 28rpx;
				line-height: 60rpx;
				margin-left: 20rpx;
				padding: 0 20rpx;
			}
		}
	}
</style>
